package cn.wolfcode.luowowo.service.impl;

import ch.qos.logback.core.db.dialect.DBUtil;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyCatalog;
import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.repository.StrategyRepository;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
//@Transactional  暂时先别管事务
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyRepository repository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IDestinationService destinationService;


    public Strategy get(String id) {
        Optional<Strategy> optional = repository.findById(id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    //查询出页面结果集
    public Page<Strategy> query(StrategyQuery qo) {
        Query query = new Query();
        if(StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        if(StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        return DBHelper.query(template, Strategy.class, query, qo.getPageable());
    }

    public void saveOrUpdate(Strategy strategy) {
        //themename
        StrategyTheme strategyTheme = strategyThemeService.get(strategy.getThemeId());
        strategy.setThemeName(strategyTheme.getName());
        //catalogname
        StrategyCatalog strategyCatalog = strategyCatalogService.get(strategy.getCatalogId());
        strategy.setCatalogName(strategyCatalog.getName());
        //destname
        strategy.setDestName(strategyCatalog.getDestName());
        //destId
        strategy.setDestId(strategyCatalog.getDestId());
        //isabroad
        List<Destination> toasts = destinationService.getToasts(strategyCatalog.getDestId());
        if("中国".equals(toasts.get(0).getName())){
            strategy.setIsabroad(Strategy.ABROAD_NO);
        }else{
            strategy.setIsabroad(Strategy.ABROAD_YES);
        }

        if(StringUtils.hasLength(strategy.getId())){
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(strategy.getId()));
            DBHelper.update(template,query,strategy,"destId","destName","themeId","themeName","catalogId",
                    "catalogName","title","subTitle","summary","coverUrl","isabroad","state","content");
        }else{
            strategy.setId(null);
            //createTime
            strategy.setCreateTime(new Date());
            repository.save(strategy);
        }
    }

    public void delete(String id) {
        repository.deleteById(id);
    }


    @Override
    public List<Strategy> list() {
        return repository.findAll();
    }

    @Override
    public void changeState(int state, String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("state",state);
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> findByCatalogId(String id) {
        return repository.findByCatalogId(id);
    }

    @Override
    public List<Strategy> viewnumTop3(String destId) {
        PageRequest pr = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");
        return repository.findByDestId(destId,pr);
    }

    @Override
    public void updateStrategyStaticVO(StrategyStatisVO vo) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(vo.getStrategyId()));
        Update update = new Update();
        update.set("viewnum",vo.getViewnum());
        update.set("favornum",vo.getFavornum());
        update.set("thumbsupnum",vo.getThumbsupnum());
        update.set("replynum",vo.getReplynum());
        update.set("sharenum",vo.getSharenum());
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> findByDestName(String destName) {
        return repository.findByDestName(destName);
    }

}
